const BaseHandler = require('../base-handler');
const SystemServices = require('../../../constants').SystemServices;
const HTTP_VERBS = require('../../../constants').HTTP_VERBS;

class SettingsUserHandler extends BaseHandler {
    get pattern() {
        return "/operation/user/:action";
    }

    doGET(request, response) {

    }

    doPOST(request, response) {
        if (!request.session.user.authenticated) {
            response.json({
                success: false,
                error: '请登录系统后再调用此接口！'
            });
        } else {
            var type = request.params.action;
            var bodyData = request.body, params;
            if (type == 'roles') {
                params = { data: { userId: bodyData.userId, roleIds: bodyData.roleIds } };
            } else {
                params = { employeId: bodyData.employeId, employeName: bodyData.employeName, mobile: bodyData.mobile, email: bodyData.email, role: bodyData.role || 0, storeId: bodyData.assign && bodyData.assign.storeId || request.session.user.storeId, type };
            }
            this.Controller.operateUser(params, (result) => {
                if (result.error) {
                    return response.json({
                        success: false,
                        error: result.error
                    });
                }
                // if (type == 'roles')
                //     //更新用户菜单权限
                //     request.session.user.roleIds = bodyData.roleIds;
                response.json({
                    success: true,
                    result: result
                });
            })
        }
    }

    setup() {
        this.registerVerb(HTTP_VERBS.GET, this.doGET);
        this.registerVerb(HTTP_VERBS.POST, this.doPOST)
    }
}

module.exports = SettingsUserHandler;